//
//  THJTradeContractVC2.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/8/21.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJTradeContractVC2.h"
#import "THJContractDetailVC.h"


@interface THJTradeContractVC2 ()
{
    NSMutableArray * tfArr;
}
@end

@implementation THJTradeContractVC2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    tfArr = [NSMutableArray new];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"确认合同"];
    }else{
        [self setNavBarWithTitle:@"确认合同"];
    }
    for(UITextField * tf in self.view.subviews){
        if([tf isKindOfClass:[UITextField class]]){
             tf.layer.borderWidth = 0.5;
        }
       
    }
    [self getData];
}
-(void)setNavBarWithTitle:(NSString*)title{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    view.backgroundColor = COLOR(83, 84, 85, 1);
    [self.view addSubview:view];
    
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(15, 20, 24, 24);
    [btn setBackgroundImage:kGetImage(@"返回") forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    
    UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth-150)/2, 20, 150, 21)];
    lab.textColor = SingleColor(whiteColor);
    lab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lab];
    lab.text = title;
    
}
-(void)getData{
    NSDictionary * dic = @{@"mobile":[RSA encryptString:[THJTools getMobile] publicKey:RSA_PubKey],@"type":@"ios"};
    NSLog(@"tijiaoLL-%@",dic);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=tax_info" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"税务信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            NSArray * arr = @[@"accno",@"address",@"dailiren",@"faren",@"shuihao",@"tel",@"yinhang",@"truename"];
            for (NSInteger i = 0; i<arr.count; i++) {
                NSString * str = [RSA decryptString:receiveData[@"data"][arr[i]] publicKey:RSA_PubKey];
                NSLog(@"styr-%@",str);
                UITextField * tf = [self.view viewWithTag:100+i];
                tf.text  = str;
            }
        }else{
//            [self HUDShowMsg:@"获取信息失败" AfterDelayDimiss:1];
        }
    }];
}
- (IBAction)sureBtnAction:(id)sender {
    if(tfArr.count){
        [tfArr removeAllObjects];
    }
    for(UITextField * tf in self.view.subviews){
        if([tf isKindOfClass:[UITextField class]]){
            [tfArr addObject:tf.text];
            if(tf.text.length == 0){
                [self HUDShowMsg:@"请完善信息" AfterDelayDimiss:1];
                return;
            }
        }
    }
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定合同内容无误并且订购吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了按钮1，进入按钮1的事件");
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        [self postInfo];
    }];
    
    
    
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    
    
    [self presentViewController:actionSheet animated:YES completion:nil];
    
}
-(void)postInfo{
    [self SVHUDShow];
    
    NSDictionary * dic = @{
                           @"type":@"ios",
                           @"itemid":[RSA encryptString:self.orderId publicKey:RSA_PubKey],
                           @"truename":[RSA encryptString:tfArr[0] publicKey:RSA_PubKey],
                           @"address":[RSA encryptString:tfArr[1] publicKey:RSA_PubKey],
                           @"faren":[RSA encryptString:tfArr[2] publicKey:RSA_PubKey],
                           @"dailiren":[RSA encryptString:tfArr[3] publicKey:RSA_PubKey],
                           @"tel":[RSA encryptString:tfArr[4] publicKey:RSA_PubKey],
                           @"accno":[RSA encryptString:tfArr[5] publicKey:RSA_PubKey],
                           @"yinhang":[RSA encryptString:tfArr[6] publicKey:RSA_PubKey],
                           @"shuihao":[RSA encryptString:tfArr[7] publicKey:RSA_PubKey],
                           @"yanshoubiaozhun":tfArr[8],
                           @"fine_standard":tfArr[9],
                           };
    NSLog(@"tijiaoLL-%@-orderid:%@",tfArr,self.orderId);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_buyer" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"买家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self HUDShowMsg:@"订购成功!" AfterDelayDimiss:1];
            [self performSelector:@selector(popRoot) withObject:nil afterDelay:1];
            
        }else{
            [self HUDShowMsg:@"订购失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)popRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
    
//    NSDictionary * dic = @{
//                           @"itemid":[RSA encryptString:self.orderId publicKey:RSA_PubKey],
//                           @"truename":[RSA encryptString:tfArr[0] publicKey:RSA_PubKey],
//                           @"address":[RSA encryptString:tfArr[1] publicKey:RSA_PubKey],
//                           @"faren":[RSA encryptString:tfArr[2] publicKey:RSA_PubKey],
//                           @"daliren":[RSA encryptString:tfArr[3] publicKey:RSA_PubKey],
//                           @"tel":[RSA encryptString:tfArr[4] publicKey:RSA_PubKey],
//                           @"accno":[RSA encryptString:tfArr[5] publicKey:RSA_PubKey],
//                           @"yinhang":[RSA encryptString:tfArr[6] publicKey:RSA_PubKey],
//                           @"shuihao":[RSA encryptString:tfArr[7] publicKey:RSA_PubKey],
//                           @"yanshoubiaozhun":tfArr[8],
//                           @"fine_standard":tfArr[9],
//                           };
//    NSLog(@"tijiaoLL-%@-orderid:%@",tfArr,self.orderId);
//    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_buyer" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
//        NSLog(@"买家合同信息--%@",receiveData);
//        [self SVHUDHiden];
//        if([receiveData[@"status"] isEqual:@"success"]){
////            [self setContractWithDic:receiveData[@"data"]];
//        }else{
//            [self HUDShowMsg:@"订购失败" AfterDelayDimiss:1];
//        }
//    }];

- (IBAction)contractDetailAction:(id)sender {
    if(tfArr.count){
        [tfArr removeAllObjects];
    }
    for(UITextField * tf in self.view.subviews){
        if([tf isKindOfClass:[UITextField class]]){
            [tfArr addObject:tf.text];
            if(tf.text.length == 0){
                [self HUDShowMsg:@"请完善信息" AfterDelayDimiss:1];
                return;
            }
        }
    }
    THJContractDetailVC * vc = [[THJContractDetailVC alloc]init];
    vc.infoArr = tfArr;
    vc.orderId = self.orderId;
    [self.navigationController pushViewController:vc animated:YES];
//    THJContractDetailVC * vc = [[THJContractDetailVC alloc]init];
//    vc.infoArr = tfArr;
//    [self.navigationController pushViewController:vc animated:YES];
//    THJContractDetailVC * vc = [[THJContractDetailVC alloc]init];
//    vc.infoArr = tfArr;
//    vc.orderId = self.orderId;
//    [self.navigationController pushViewController:vc animated:YES];
}

@end
